package com.one.rope.mvp.web.controller.admin;

import com.one.rope.mvp.basecore.annotation.LogMethod;
import com.one.rope.mvp.web.business.service.IMemberPushTaskLogService;
import com.one.rope.mvp.web.common.dto.admin.push.*;
import com.one.rope.mvp.basecore.bean.ResponseObject;
import com.one.rope.mvp.web.common.util.ObjectUtils;
import com.one.rope.mvp.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Weijian.Liu
 * @desc 管理后台-会员用户推送管理控制器
 * @date 2020/2/20 22:53
 */
@Api(tags = "A4管理后台-推送管理模块")
@RestController
@RequestMapping("/admin/api")
@Slf4j
public class AdminMemberPushTaskController extends BaseController {

  @Autowired
  IMemberPushTaskLogService memberPushTaskLogService;

  @LogMethod
  @ApiOperation(value = "001会员推送任务列表", notes = "会员推送任务列表接口", response = AdminMemberPushListItemVO.class)
  @PostMapping("/push/pushTaskList")
  public ResponseObject pushTaskList(@RequestBody AdminMemberPushListDTO requestDTO) {
    try {
      return memberPushTaskLogService.pushTaskList(requestDTO);
    } catch (Exception e) {
      log.error("/admin/api/push/pushTaskList接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "002推送页面列表(已废弃)", notes = "推送页面列表接口", response = AdminMemberPushPageVO.class)
  @GetMapping("/push/pushPageList")
  public ResponseObject pushPageList() {
    try {
      return memberPushTaskLogService.pushPageList();
    } catch (Exception e) {
      log.error("/admin/api/push/pushPageList接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "003保存推送任务", notes = "保存推送任务接口")
  @PostMapping("/push/savePushTask")
  public ResponseObject savePushTask(@RequestBody AdminSaveMemberPushDTO requestDTO) {
    ObjectUtils.requestNonEmpty(requestDTO.getName(), "任务名称不能为空");
    ObjectUtils.requestNonEmpty(requestDTO.getPageUrl(), "推送页面不能为空");
    ObjectUtils.requestNonNull(requestDTO.getPushTarget(), "推送目标不能为空");
    try {
      return memberPushTaskLogService.savePushTask(requestDTO);
    } catch (Exception e) {
      log.error("/admin/api/push/savePushTask接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "004推送任务", notes = "推送接口", response = AdminMemberPushVO.class)
  @GetMapping("/push/executePush/{id}")
  public ResponseObject<AdminMemberPushVO> executePush(@PathVariable("id") Long id) {
    try {
      return memberPushTaskLogService.executePush(id);
    } catch (Exception e) {
      log.error("/admin/api/push/executePush接口异常", e);
      return ResponseObject.systemError(e);
    }
  }
}
